﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace ImageControl.Common
{
    public class CoordinatesFormater : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = null;
            double coordinate = (double)value;
            int digits = 6;
            string trimmed = String.Format("{0:G" + digits + "}", coordinate);

            if (trimmed.Length < digits && !double.IsNaN(coordinate) && !double.IsInfinity(coordinate))
            {
                int integer = (int)coordinate;
                if (coordinate == integer)
                {
                    if (integer.ToString().Length < digits - 1)
                    {
                        result = String.Format("{0:F" + digits + "}", integer);
                    }
                }
                else
                {
                    result = trimmed.PadRight(digits+1, '0');
                }
            }
                
            if(result == null)
            {
                result = trimmed;
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
